package com.fauks.crm.controller;

import com.fauks.crm.pojo.BaseDict;
import com.fauks.crm.pojo.Customer;
import com.fauks.crm.pojo.QueryVo;
import com.fauks.crm.service.BaseDictService;
import com.fauks.crm.service.CustomerService;
import com.fauks.crm.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 客户信息请求处理
 */
@Controller
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private BaseDictService baseDictService;

    @Autowired
    private CustomerService customerService;

    //属性文件注入
    @Value("${customer_from_type}")
    private String customer_from_type;

    @Value("${customer_industry_type}")
    private String customer_industry_type;

    @Value("${customer_level_type}")
    private String customer_level_type;

    @RequestMapping("list")
    public String list(Model model, QueryVo queryVo) {
        //封装数据字典数据
        //硬编码方式不灵活采用配置文件方式
        //List<BaseDict> fromType = baseDictService.getBaseDictByCode("002");
        List<BaseDict> fromType = baseDictService.getBaseDictByCode(customer_from_type);
        List<BaseDict> industryType = baseDictService.getBaseDictByCode(customer_industry_type);
        List<BaseDict> levelType = baseDictService.getBaseDictByCode(customer_level_type);
        model.addAttribute("fromType", fromType);
        model.addAttribute("levelType", levelType);
        model.addAttribute("industryType", industryType);

        Page<Customer> page = customerService.getCustomerByQueryVo(queryVo);
        model.addAttribute("page", page);
        return "customer";
    }

    @RequestMapping("edit")
    @ResponseBody
    public Customer editCustomerUI(Integer id) {
        Customer customer = customerService.getCustomerById(id);
        return customer;
    }

    @RequestMapping("update")
    @ResponseBody
    public String updateCustomerr(Customer customer) {
        String msg = "1";
        try {
            customerService.updateCustomer(customer);
            msg = "0";
        } catch (Exception e) {
        }
        return msg;
    }

}
